package exam.pdd.S2;


import java.util.*;

public class Main {

    static class Node {
        int k;
        int a;
        int b;
    }

    public static int T;
    public static int n;
    public static int v;
    public static Queue<Node> pq = new PriorityQueue<>((t1, t2) -> {
        if (t1.b != t2.b)
            return t1.b - t2.b;
        else if (t1.a != t2.a)
            return t1.a - t2.a;
        return t1.k - t2.k;
    });

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        T = sc.nextInt();
        for (int i = 0; i < T; i++) {
            solution(sc);
        }
    }

    public static void inputParams(Scanner sc) {
        n = sc.nextInt();
        v = sc.nextInt();
        pq.clear();
        for (int i = 0; i < n; i++) {
            Node node = new Node();
            node.k = sc.nextInt();
            node.a = sc.nextInt();
            node.b = sc.nextInt();
            pq.offer(node);
        }
    }

    public static void solution(Scanner sc) {
        inputParams(sc);
        int res = 0, cur = 1, hold = 0;
        while (!pq.isEmpty()) {
            Node top = pq.peek();
            if (top.b < cur) {
                pq.poll();
                continue;
            }
            if (top.a > cur) {
                cur = top.a;
                res += hold;
                hold = 0;
                continue;
            }
            top = pq.poll();
            if (top.k <= v - hold) {
                hold += top.k;
            } else {
                top.k -= (v-hold);
                hold = v;
                pq.offer(top);
            }
            if (hold == v) {
                cur++;
                hold = 0;
                res += v;
            }
        }
        res += hold;
        System.out.println(res);
    }
}